﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

/// <summary>
/// 1. 设置或切换平面检测模式
/// 2. 切换平面检测是否可用
/// 3. 显示或隐藏平面可视化
/// </summary>
public class PlaneManager : MonoBehaviour
{
    private ARPlaneManager m_ARPlaneManager = null;

    private void Awake()
    {
        m_ARPlaneManager = FindObjectOfType<ARPlaneManager>();
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {

    }

    /// <summary>
    /// 设置平面检测的几种模式:
    ///     PlaneDetectionMode.Horizontal
    ///     PlaneDetectionMode.Vertical
    ///     PlaneDetectionMode.Horizontal | PlaneDetectionMode.Vertical
    /// </summary>
    /// <param name="mode"></param>
    public void SetPlaneDetectionMode(PlaneDetectionMode mode) 
    {
        m_ARPlaneManager.detectionMode = mode;
    }

    public string GetPlaneDetectionModeStr()
    {
        if (m_ARPlaneManager.detectionMode == PlaneDetectionMode.Horizontal)
        {
            return "PlaneDetectionMode.Horizontal";
        }
        else
        {
            return "PlaneDetectionMode.Vertical";
        }
    }

    /// <summary>
    /// 切换平面检测模式(水平/垂直)
    /// </summary>
    public void TogglePlaneDetectionMode()
    {
        if(m_ARPlaneManager.detectionMode == PlaneDetectionMode.Horizontal)
        {
            m_ARPlaneManager.detectionMode = PlaneDetectionMode.Vertical;
        }
        else
        {
            m_ARPlaneManager.detectionMode = PlaneDetectionMode.Horizontal;
        }

        ToastUtil.showToast("PlaneManager togglePlaneDetectionMode() detectionMode=" + GetPlaneDetectionModeStr());
    }

    /// <summary>
    /// 切换平面检测是否可用
    /// </summary>
    public void TogglePlaneDetectionEnabled()
    {
        m_ARPlaneManager.enabled = !m_ARPlaneManager.enabled;

        string planeDetectionMessage = "";
        if (!m_ARPlaneManager.enabled)
        {
            planeDetectionMessage = "设置了平面检测 不可用";
            SetAllPlanesActive(false);
        }
        else
        {
            planeDetectionMessage = "设置了平面检测 可用";
            SetAllPlanesActive(true);
        }

        ToastUtil.showToast("planeDetectionMessage = " + planeDetectionMessage);
    }

    /// <summary>
    /// 显示或隐藏平面可视化
    /// </summary>
    private void SetAllPlanesActive(bool value)
    {
        foreach (var plane in m_ARPlaneManager.trackables)
            plane.gameObject.SetActive(value);
    }

}
